using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Comteck.Dto.Orders;
using Comteck.Dto.Sys;
using Comteck.Ktt.Services.Orders;
using Comteck.Ktt.Services.Sys;
using Comteck.Localization;
using Comteck.Winforms.Forms;
using DevExpress.XtraBars;

namespace Comteck.Ktt.WinUI.Orders {
  /// <summary>
  /// 订单商品拆分
  /// </summary>
  public partial class FrmSaleOrderDetailSplitAction : FrmEditBase {
    private readonly ISaleOrderService saleOrderService;
    private readonly IParameterService parameterService;

    /// <summary>
    /// 
    /// </summary>
    [Browsable(false)]
    public SaleOrderDto OrderDto { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [Browsable(false)]
    public SaleOrderDetailDto OrderDetailDto { get; set; }

    /// <summary>
    /// 确定的回调方法
    /// </summary>
    [Browsable(false)]
    public BoolAction<List<SaleOrderDto>> ImportAction { get; set; }

    /// <summary>
    /// 确定的回调方法
    /// </summary>
    [Browsable(false)]
    public BoolActionAsync<List<SaleOrderDto>> ImportActionAsync { get; set; }


    /// <summary>
    /// 
    /// </summary>
    public FrmSaleOrderDetailSplitAction(ISaleOrderService saleOrderService,
      IParameterService parameterService) {
      this.saleOrderService = saleOrderService;
      this.parameterService = parameterService;
      InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmSaleOrderDetailSplitAction_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      ckCode.EditValue = this.OrderDto.Code;
      ckGroupNumber.EditValue = this.OrderDto.GroupNumber;
      ckGroupTitle.EditValue = this.OrderDto.GroupTitle;

      ckProductName.EditValue = this.OrderDetailDto.ProductName;
      ckQty.EditValue = this.OrderDetailDto.Qty;
      ckWeight.EditValue = this.OrderDetailDto.Weight;
      ckTotalWeight.EditValue = this.OrderDetailDto.TotalWeight;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ckNewQty_EditValueChanged(object sender, EventArgs e) {
      // 每份数量
      var newQty = ckNewQty.EditValue.To<int>();

      // 每份总重量
      ckNewTotalWeight.EditValue = newQty * this.OrderDetailDto.Weight;

      // 生成订单数
      ckNewOrderCount.EditValue = (int) Math.Ceiling((double) this.OrderDetailDto.Qty / newQty);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnSave_ItemClick(object sender, ItemClickEventArgs e) {
      // 每份数量
      var newQty = ckNewQty.EditValue.To<int>();
      if (newQty <= 0) {
        this.ShowMsg("每份数量必须大于0");
        return;
      }

      // 限重
      var order_weight = (await parameterService.GetValueAsync(EnumParameter.order_weight)).To(10m);
      var newTotalWeight = newQty * this.OrderDetailDto.Weight;
      var msg = newTotalWeight <= order_weight ? BaseResources.Execute_Confirm : $"拆分后每份订单的重量 {newTotalWeight} 超过限重 {order_weight}，您确定还要生成吗？";

      var dialog = this.ShowMsg(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
      if (dialog == DialogResult.No) {
        return;
      }

      ajaxLoad.Show();
      var result = await saleOrderService.SplitOrderDetailAsync(this.OrderDetailDto.OrderId,  this.OrderDetailDto.Id, newQty);
      ajaxLoad.Hide();

      if (result.Code == 1) {
        this.ShowMsg($"拆单成功，共生成 {ckNewOrderCount.EditValue.To<int>()} 个订单");

        // import
        if (this.ImportAction != null) {
          if (this.ImportAction?.Invoke(result.Data) == true) {
            this.DialogResult = DialogResult.OK;
          }
        } else if (this.ImportActionAsync != null) {
          if ((await this.ImportActionAsync(result.Data)) == true) {
            this.DialogResult = DialogResult.OK;
          }
        }
      } else {
        this.ShowMsg($"拆单失败，{result.Message}");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }
  }
}
